<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\News;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class NewsController extends Controller
{
    public function lists(Request $request): JsonResponse
    {
        return $this->success(
            News::query()
                ->when($request->input('title'), function ($query, $title) {
                    $query->where('title', 'like', '%' . $title . '%');
                })
                ->orderByDesc('id')
                ->paginate($request->input('limit', 10))
        );
    }

    public function updateOrCreate(Request $request): JsonResponse
    {
        try {
            $this->validate($request, [
                'title' => 'required',
                'image' => 'required',
                'content' => 'required',
            ], [
                'title.required' => '请输入标题',
                'image.required' => '请上传图片',
                'content.required' => '请输入内容',
            ]);
        } catch (ValidationException $e) {
            return $this->error($e->validator->errors()->first());
        }
        $res = News::query()->updateOrCreate(['id' => $request->input('id')], [
            'title' => $request->input('title'),
            'author' => $request->input('author'),
            'image' => $request->input('image'),
            'content' => $request->input('content'),
            'description' => $request->input('description'),
            'published_at' => $request->input('published_at', date('Y-m-d H:i:s')),
        ]);
        return $res === false ? $this->error('操作失败') : $this->success();
    }

    public function delete(Request $request): JsonResponse
    {
        $res = News::query()->where('id', $request->input('id'))->delete();
        return $res === false ? $this->error('操作失败') : $this->success();
    }

    public function categoryList(Request $request): JsonResponse
    {
        return $this->success(
            Category::query()
                ->when($request->input('name'), function ($query, $name) {
                    $query->where('name', 'like', '%' . $name . '%');
                })
                ->orderByDesc('id')
                ->paginate($request->input('limit', 10))
        );
    }

    public function categoryUpdateOrCreate(Request $request): JsonResponse
    {
        try {
            $this->validate($request, [
                'name' => 'required',
            ], [
                'name.required' => '请输入类目名称',
            ]);
        } catch (ValidationException $e) {
            return $this->error($e->validator->errors()->first());
        }
        $res = Category::query()->updateOrCreate(['id' => $request->input('id')], [
            'name' => $request->input('name'),
            'url' => $request->input('url'),
            'swipers' => implode(',', $request->input('swipers', [])),
        ]);
        return $res === false ? $this->error('操作失败') : $this->success();
    }

    public function categoryDelete(Request $request): JsonResponse
    {
        $res = Category::query()->where('id', $request->input('id'))->delete();
        return $res === false ? $this->error('操作失败') : $this->success();
    }
}
